#include "qxreporttable.h"

QXReportTable::QXReportTable(QObject *parent) : QObject(parent)
{

}

QXReportTableCell *QXReportTable::cell(int row, int column)
{
    QXReportTableCell* _cell = nullptr;

    if(m_cellList.contains(row))
    {
        if(m_cellList.value(row).contains(column))
        {
            _cell = m_cellList[row][column];
        }
    }

    return _cell;
}

// 添加单元格
void QXReportTable::addCell(int row, int column, QXReportTableCell *_cell)
{
    if(_cell == nullptr)
    {
        return;
    }

    // 非空时，将单元格添加到表格
    if(!m_cellList.contains(row))
    {
        QMap<int, QXReportTableCell*> rowCells;
        m_cellList.insert(row, rowCells);
    }

    m_cellList[row].insert(column, _cell);
}

// 获取行高
int QXReportTable::getRowMinHeight(const QFont &font, int iRow)
{
    int height = 10;

    if(m_cellList.contains(iRow))
    {
        foreach(int col, m_cellList.value(iRow).keys())
        {
            QXReportTableCell* _cell = m_cellList[iRow][col];
            if(_cell != nullptr)
            {
                if(_cell->rowSpan() == 1)
                {
                    int w = 100;
                    if(m_columnWidthMap.contains(col))
                    {
                        w = m_columnWidthMap.value(col);
                    }

                    int cellMinHeight = _cell->getTextMinHight(font, Qt::TextWordWrap, w);
                    if(cellMinHeight > height)
                    {
                        height = cellMinHeight;
                    }
                }
            }
        }
    }

    return height;
}

// 获取列数
int QXReportTable::columnCount()
{
    return m_iColumnCount;
}

// 设置列数
void QXReportTable::setColumnCount(int n)
{
    m_iColumnCount = n;
}

// 设置列宽
void QXReportTable::setColumnWidth(int iColumn, int width)
{
    if(m_columnWidthMap.contains(iColumn))
    {
        m_columnWidthMap[iColumn] = width;
    }
    else
    {
        m_columnWidthMap.insert(iColumn, width);
    }
}
